package in.muton.edushare.poll.sections;

import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.pages.user.UserPolls;
import in.muton.edushare.poll.panels.SummaryPanel;
import in.muton.edushare.wicket.models.CayenneModel;

import java.util.List;

import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.JavascriptPackageResource;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.IModel;

import com.edushare.common.dao.UserPollDAO;
import com.edushare.common.vo.Poll;
import com.edushare.common.vo.PollPages;

public class PollRespondPanel extends EdusharePanel {
	private static final long serialVersionUID = 8013140579434368942L;
	private IModel<Poll> pollModel;

	public PollRespondPanel(String id, IModel<Poll> model,
			final EduShareBasePage page, final boolean preview) {
		super(id, page);
		pollModel = model;
		add(JavascriptPackageResource.getHeaderContribution(
				PollRespondPanel.class, "PollSection.js"));
		add(CSSPackageResource.getHeaderContribution(PollRespondPanel.class,
				"PollSection.css"));

		Form<Void> f = new Form<Void>("questionsForm");
		add(f);
		final PollSection p = new PollSection("pollCurrentPage", pollModel,
				page, 1, preview);
		f.add(p);

		add(new SummaryPanel("pollSummary", pollModel, page));

		f.add(new Button("vote") {
			private static final long serialVersionUID = 4567146075757845753L;

			@Override
			public void onSubmit() {
				if(preview)
					return;
				if (p.save(false)) {
					new UserPollDAO(PollRespondPanel.this.getORMInterface())
							.setPollAsDone(getLoggedInUser(), pollModel
									.getObject());
					setResponsePage(UserPolls.class);
				}
			}

			@Override
			public boolean isVisible() {
				if(preview)
					return false;
				return isLastPage(p.getCurrentPage());
			}
		});

		f.add(new Button("prev") {
			private static final long serialVersionUID = 4567146075757845753L;

			@Override
			public void onSubmit() {
				p.prevPage();
			}

			@Override
			public boolean isVisible() {
				return !isFirstPage(p.getCurrentPage());
			}
		});

		f.add(new Button("next") {
			private static final long serialVersionUID = 4567146075757845753L;

			@Override
			public void onSubmit() {
				if (preview || p.save(false))
					p.nextPage();
			}

			@Override
			public boolean isVisible() {
				return !isLastPage(p.getCurrentPage());
			}
		});

		f.add(new Button("draft") {
			private static final long serialVersionUID = 4567146075757845753L;

			@Override
			public void onSubmit() {
				if(preview)
					return;
				p.save(true);
				page.setErrorMessage("Your responses have been saved."
						+ "You may return to this page and complete "
						+ "the survey anytime before the survey close date.");
			}

			@Override
			public boolean isVisible() {
				if(preview)
					return false;
				return !new UserPollDAO(getORMInterface()).isPollDone(
						getLoggedInUser(), pollModel.getObject());
			}
		});

		f.add(new Button("clear") {
			private static final long serialVersionUID = 4567146075757845753L;

			@Override
			public void onSubmit() {
				p.clear();
			}
		});

	}

	public static IModel<PollPages> getNextPage(IModel<PollPages> currPage) {
		List<PollPages> pageList = currPage.getObject().getPoll().getPages();
		for (PollPages page : pageList) {
			if (page.getPageNumber() == currPage.getObject().getPageNumber() + 1)
				return new CayenneModel<PollPages>(page);
		}
		return null;
	}

	public static boolean isFirstPage(IModel<PollPages> pageModel) {
		if (pageModel.getObject().getPageNumber() == 1)
			return true;
		return false;
	}

	public static boolean isLastPage(IModel<PollPages> pageModel) {
		if (getNextPage(pageModel) == null)
			return true;
		return false;
	}
}
